﻿using Microsoft.EntityFrameworkCore;

namespace BaWei.Rabbit.Domain
{
    public class RabbitDbContext:DbContext
    {
        //:base  调用的父类构造函数
        public RabbitDbContext(DbContextOptions dbContextOptions):base(dbContextOptions)
        {
            
        }

        public DbSet<Student> Students { get; set; }

        //父类的什么方法可以被重写？ virtual（虚方法）
        //override 重写父类方法
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            
            base.OnConfiguring(optionsBuilder);
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
           
            //给实体添加表名
            modelBuilder.Entity<Student>().ToTable("Student");
            //给实体添加主键
            modelBuilder.Entity<Student>().HasKey(s => s.Id);
            //给实体Name添加长度限制   IsRequired  不能为空
            modelBuilder.Entity<Student>().Property(s => s.Name).IsRequired().HasMaxLength(50);
            
            
            base.OnModelCreating(modelBuilder);
        }
    }
}
